<?

class poll
{
    # Show options function
    function showOptions()
    {
        # Get newest poll
        $data = $this->getPoll();

        # Split options
        $options = explode(", ", $data['poll_options']);
        # Count options for loop
        $aant = count($options)-1;

        $poll .= "<p><b>" . $data['poll_question'] . "</b></p>";
        $poll .= "<form method=\"post\" action=\"\">";
        $poll .= "<fieldset>";
        # Loop and make input radios
        for($i = 0; $i <= $aant; $i++)
        {
            $j = $i+1;
            $poll .= "<input type=\"radio\" value=\"" . $j . "\" name=\"poll\" /><span class=\"count\">" . $options[$i] . "</span><br />";
        }
        # End form
        $poll .= "<br /><input type=\"submit\" name=\"vote\" value=\"Vote\" />";
        $poll .= "</fieldset>";
        $poll .= "</form>";

        return $poll;
    }

    function updateVotes($vote)
    {
        # Get newest poll
        $data = $this->getPoll();

        # Split the vote scores
        $stemmen = explode(",", $data['poll_score']);

        # add one to the selected option
        $vote--;
        (int) $stemmen[$vote]++;

        # Remake the data
        $stem = implode(", ", $stemmen);

        # Add to database
        $sql1 = "UPDATE `poll` SET `poll_score` = '" . $stem . "' WHERE `poll_id` = '" . $data['poll_id'] . "'";
        $ip = $_SERVER['REMOTE_ADDR'];
        $sql2 = "INSERT INTO `votes` SET `vote_ip` = '" . $ip . "', `vote_date` = '" . time() . "'";

        # Run db class
        $db = new database;

        $res1 = $db->execute($sql1);
        $res2 = $db->execute($sql2);

    }

    function getPoll()
    {
        # Select the newest poll
        $sql = "SELECT * FROM ".$prefix."`poll` ORDER BY `poll_id` DESC LIMIT 1";

        # Run db class
        $db = new database;

        # Get array
        $data = $db->get_single($sql);

        return $data;
    }

    function showResults()
    {
        # Get data
        $data = $this->getPoll();

        # Split options and scores
        $options = explode(", ", $data['poll_options']);
        $scores = explode(",", $data['poll_score']);

        $option = count($options)-1;

        # Count the votes
        $votes = array_sum($scores);

        # Show total votes
        $results .= "<p><b>" . $data['poll_question'] . "</b><br />";
        $results .= "Total number of votes: " . $votes . "</p>";
        $results .= "<p>";

        # Loop through results
        for($i = 0;$i <= $option; $i++)
        {
            $results .= $options[$i] . "<br />";
            $results .= "<img src=\"../../../content/themes/softsaurus_default/images/indicator.png\" width=\"" . $this->imgPXL($votes, $scores[$i]) . "\" height=\"10\" alt=\"graph graphic\" />" . $this->imgPXL($votes, $scores[$i]) . "%";
            $results .= "<br />";
        }
        $results .= "</p>";

        return $results;
    }

    # Calculate indicator size
    function imgPXL($tot, $opt)
    {
        $pro = $tot / 100;
        $num = $opt / $pro;

        return (int) $num;
    }
}
?> 